# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Contributor: Baptiste Jonglez <baptiste--aur at jonglez dot org>

_pkgname=lablgtk
pkgname="${_pkgname}3"
pkgver=3.1.3
pkgrel=3
pkgdesc="An Objective Caml interface to gtk3"
arch=('loong64' 'x86_64')
license=('LGPL')
url="http://lablgtk.forge.ocamlcore.org/"
depends=('gtk3' 'gtksourceview3' 'gtkspell3' 'ocaml-cairo')
makedepends=('ocaml-compiler-libs' 'ocaml-findlib' 'dune' 'camlp-streams')
optdepends=('ocaml: for using the tools')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/garrigue/lablgtk/archive/refs/tags/${pkgver}.tar.gz")
b2sums=('639b1a7610ea420cbae893eb40fd0a20a7605ecc89e832387f3137a002fd4989342ca842c1c7e9057454e0b6cfac9099c5788b5ea14fa452bde54bacb18aeac2')

_dune_release_pkgs=('lablgtk3' 'lablgtk3-gtkspell3' 'lablgtk3-sourceview3')
dune_release_pkgs_comma() {
  # dune build needs a comma separated list of packages
  local dune_release_pkgs='dummy'
  for _pkg in "${_dune_release_pkgs[@]}"; do
    dune_release_pkgs+=",${_pkg}"
  done
  dune_release_pkgs="${dune_release_pkgs#dummy,}"
  echo "${dune_release_pkgs}"
}

build() {
  cd "${srcdir}/${_pkgname}-${pkgver}"
  # gcc-10 workaround
  export OCAMLPATH="$(ocamlfind printconf destdir)"
  LABLGTK_EXTRA_FLAGS=-fcommon dune build -p $(dune_release_pkgs_comma)
}

package() {
  cd "${srcdir}/${_pkgname}-${pkgver}"
  # we need to install packages one by one
    for _pkg in "${_dune_release_pkgs[@]}"; do
      dune install "${_pkg}" --destdir="${pkgdir}" --prefix="/usr" --libdir="$(ocamlfind printconf destdir)" 
    done

  install -dm755 "${pkgdir}/usr/share/"
  mv "${pkgdir}/usr/doc" "${pkgdir}/usr/share/"
}
